<template>
  <div class="login_container">
    <div class="login_box">
      <!-- 头像 -->
      <div class="avatar_box">
        <img src="../assets/logo.png" alt="">
      </div>
      <!-- 表单 -->
      <el-form ref="loginform" class="login_form" :model="form" :rules="rules">
        <el-form-item prop="username">
          <el-input prefix-icon="iconfont icon-zhanghu" v-model="form.username" placeholder="请输入账户名称"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input prefix-icon="iconfont icon-mima" v-model="form.password" placeholder="请输入密码"></el-input>
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary" @click="loginStar('loginform')">登录</el-button>
          <!-- <el-button type="info" @click="resetForm('loginform')">重置</el-button> -->
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { Message } from 'element-ui'
import { createLogger } from 'vuex'
// @ is an alias to /src

export default {
  name: 'login',
  data() {
    return {
      form: {
        username: localStorage.getItem('username'),
        password: localStorage.getItem('password')
      },
      rules: {
        username: [
          { required: true, message: '请输入账户名称', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 5, max: 8, message: '长度在 5 到 8 个字符', trigger: 'blur' }
        ]
      }
    }

  },
  //自调用函数
  methods: {
    loginStar(formName) {
      this.$refs[formName].validate(valid => { //async
        if (valid) {
          // const {data:res} = await this.$http.post('/index',this.form)
          this.$http.post('/abc/dm/zxyDateLogin', this.form).then(data => {
            console.log(data)
            this.confirmLogin()
          }).catch(err => {
            console.log(err)
            this.confirmLogin()
          })
        } else {
          this.$message.error('账户或密码输入错误');
          return false;
        }
      });
    },
    //重置表单
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    //完成登录
    confirmLogin() {
      localStorage.setItem('username',this.form.username)
      localStorage.setItem('password',this.form.password)
      window.sessionStorage.setItem('token', 'aaa-jdbc-a')
      this.$router.push('/index')
    }
  }
}
</script>

<style lang="less" scoped>
.login_container {
  background-color: #4b5b6b;
  height: 100%;
}

.login_box {
  width: 450px;
  height: 300px;
  background-color: #fff;
  border-radius: 5px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}

.avatar_box {
  width: 130px;
  height: 130px;
  border: 1px solid #eee;
  border-radius: 50%;
  padding: 10px;
  box-shadow: 0 0 10px #ddd;
  position: absolute;
  left: 50%;
  transform: translate(-50%, -50%);
  background-color: #fff;

  img {
    width: 100%;
    height: 100%;
    border-radius: 50%;
    background-color: #eee;
  }
}

.login_form {
  position: absolute;
  bottom: 0%;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}

.btns {
  display: flex;
  justify-content: flex-end;
}
</style>
